<!-- 我的设备 -->

<template>
  <WebForm path="map" class="MapPoint-MyIOT">
    <div class="MapPoint-MyIOT-title">
      <i class="el-icon-arrow-left" @click="toBack" />
      <span>{{ CORE.dict(DICT.IOT_TYPE, map_param.deviceCategoryId) }}</span>
    </div>

    <FormInput :one="map_param" tip="名称 / 编号" bind="keyword" medium noLabel />

    <EasyScroll>
      <div class="minFull" @mousedown.self="map_setOne" @contextmenu.prevent="CORE.doNothing">
        <div
          v-for="item in iotList"
          :key="item.id"
          :class="item.className"
          @mousedown.prevent="mouseDown($event, item)"
          @contextmenu.prevent="CORE.doNothing"
          @mouseenter="mouseEnter(item)"
          @mouseleave="mouseLeave(item)"
        >
          <CommonSvg :src="map_GETTER.isLine ? lineIcon : CORE.imageIOT(item)" />
          <span>{{ item.showName }}</span>
        </div>

        <el-empty v-if="readySelect && !iotList.length" description="暂无点位" />
      </div>
    </EasyScroll>
  </WebForm>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyIOT',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [
    CORE.mixWeb('map', 'map_'),
    CORE.mixInfo('map', 'map_'),
    CORE.mixSelect('map', 'map_'),
    CORE.mixSelect(),
  ],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 周界图标
     */
    lineIcon: () => CORE.common('iot/line.svg'),

    /**
     * IOT 列表
     */
    iotList() {
      const { select, map_GETTER, map_select, map_param, map_list, map_one, map_MAP } = this

      if (!this.map_readyInfo || !(map_GETTER.isLine ? this.map_readySelect : this.map_readyList)) return []

      const name = 'MapPoint-MyIOT-item'
      const iotList = (select[0] || []).filter((iot) => iot.showName.includes(map_param.keyword))

      return iotList.map(({ id, ...iot }) => {
        const point = map_GETTER.isLine
          ? map_select.find((item) => item.deviceId === id && !CORE.isEmpty(item.site))
          : map_list.find((item) => item.deviceId === id)

        let grayStr = point ? ` ${name}-gray` : ''
        if (map_GETTER.isVideo && point) { // 视频点位
          if (point.left === null || point.top === null) grayStr = ''
        }

        const bindStr = map_one.deviceId === id ? ` ${name}-bind` : ''
        const hoverStr = map_MAP.hoverId === id ? ` ${name}-hover` : ''

        return { ...iot, id, point, className: `${name}${grayStr}${bindStr}${hoverStr}` }
      })
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 点击返回
     */
    toBack() {
      this.map_param.keyword = ''

      this.map_param.deviceCategoryId = ''
      this.map_setList()
      this.map_setOne()

      this.map_selectParam.deviceCategoryId = ''
      this.map_setSelect()

      this.selectParam.nextIdList = []
    },

    /**
     * 点击每项
     */
    mouseDown({ button }, { deviceId, deviceName, deviceCategoryId, deviceTypeId, pointTypeCode, pointTypeName, point }) {
      if (button !== 0) return this.map_setOne() // 非左键

      const { MAP, DICT, map_setOne } = this

      // 已置灰的重新打点
      if (point) return map_setOne(point)

      // 开始打点
      map_setOne({
        deviceId,
        deviceName,
        deviceCategoryId,
        deviceTypeId,
        pointTypeCode,
        pointTypeName,
        subSystemId: CORE.dict(DICT.IOT_TYPE, deviceCategoryId, 2),
        objectType: MAP.OBJECT_TYPE[deviceCategoryId] || 1,
      })
    },

    /**
     * 鼠标进入
     */
    mouseEnter(item) {
      this.map_MAP.hoverId = item.deviceId
    },

    /**
     * 鼠标离开
     */
    mouseLeave() {
      this.map_MAP.hoverId = ''
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
